package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.customer.AddressBookApi;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 自定义Feign客户端用于熔断降级
 * @author JIAN
 */
@Slf4j
@Component
@SuppressWarnings("unused")
public class CustomerClient {
    @Resource
    private ServeApi serveApi;

    @Resource
    private AddressBookApi addressBookApi;

    @SentinelResource(value = "addressBookDetail",
            fallback = "getAddressDetailFallback", blockHandler = "getAddressDetailBlockHandler")
    public AddressBookResDTO getAddressDetail(Long id) {
        return addressBookApi.getById(id);
    }

    public AddressBookResDTO getAddressDetailFallback(Long id, Throwable throwable) {
        log.warn("地址簿接口异常(未触发熔断), 地址簿id: {}", id, throwable);
        return new AddressBookResDTO();
    }

    public AddressBookResDTO getAddressDetailBlockHandler(Long id, BlockException blockException) {
        log.warn("地址簿接口异常(触发熔断降级), 地址簿id: {}", id, blockException);
        return new AddressBookResDTO();
    }

    @SentinelResource(value = "serveAggregation",
            fallback = "getServeAggregationDetailFallback", blockHandler = "getServeAggregationDetailBlockHandler")
    public ServeAggregationResDTO getServeAggregation(Long id) {
        return serveApi.findById(id);
    }

    public ServeAggregationResDTO getServeAggregationDetailFallback(Long id, Throwable throwable) {
        log.warn("服务接口异常(未触发熔断), 服务id: {}", id, throwable);
        return new ServeAggregationResDTO();
    }

    public ServeAggregationResDTO getServeAggregationDetailBlockHandler(Long id, BlockException blockException) {
        log.warn("服务接口异常(触发熔断降级), 服务id: {}", id, blockException);
        return new ServeAggregationResDTO();
    }
}